package com.sgmw.sales.tool.kong.sign.generator;

import java.util.ArrayList;
import java.util.List;

import com.sgmw.sales.tool.kong.sign.AbstractAuthroizationGenerator;
import com.sgmw.sales.tool.kong.sign.headers.RequestHeader;
import com.sgmw.sales.tool.kong.sign.headers.SignHeaderDate;
import com.sgmw.sales.tool.kong.sign.headers.SignHeaderDigest;
import com.sgmw.sales.tool.kong.sign.headers.SignHeaderRequestLine;
import com.sgmw.sales.tool.kong.sign.request.HttpRequest;
import com.sgmw.sales.tool.kong.sign.request.body.RequestBody;

/**
 * GET和DELETE方法对：请求时间戳和请求行做签名<br>
 * POST和PUT方法对：请求时间戳,请求行,请求体做签名
 * @author hujunwei
 * @d2019年3月14日
 */
public class DefaultAuthroizationGenerator extends AbstractAuthroizationGenerator {
	private HttpRequest request;

	public DefaultAuthroizationGenerator(String username, String secret, HttpRequest request) {
		super(username, secret);
		this.request = request;
	}

	@Override
	protected List<RequestHeader> decideSignHeaders() {
		List<RequestHeader> signHeaders = new ArrayList<>();
		SignHeaderDate date = new SignHeaderDate();
		if (request.isGet() || request.isDelete()) {
			SignHeaderRequestLine requestLine = new SignHeaderRequestLine(request.getMethod(), request.getUri(),
					request.getQueryParamMap());

			signHeaders.add(date);
			
			//将请求行加入签名字符串
//			String rl = requestLine.getValue();
//			SignHeaderDigest digest = new SignHeaderDigest(rl);
//			signHeaders.add(digest);
			signHeaders.add(requestLine);
			
//			SignHeaderContentMd5 md5 = new SignHeaderContentMd5(rl);
//			signHeaders.add(md5);
		} else if (request.isPost() || request.isPut()) {
			SignHeaderRequestLine requestLine = new SignHeaderRequestLine(request.getMethod(), request.getUri(),
					request.getQueryParamMap());

			RequestBody rb = request.getRequestBody();
			if (rb != null) {
				SignHeaderDigest digest = new SignHeaderDigest(rb.getContent());
				signHeaders.add(digest);
			}

			signHeaders.add(date);
			signHeaders.add(requestLine);
		} else {

		}
		return signHeaders;
	}
}
